<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="aaton">
    
    <title>
        
            Javascript之日期时间对象 |
        
        aaton&#39;s study notes
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/logo.svg">
    
<link rel="stylesheet" href="/font/css/fontawesome.min.css">

    
<link rel="stylesheet" href="/font/css/regular.min.css">

    
<link rel="stylesheet" href="/font/css/solid.min.css">

    
<link rel="stylesheet" href="/font/css/brands.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {}
    KEEP.hexo_config = {"hostname":"example.com","root":"/","language":"en"}
    KEEP.theme_config = {"toc":{"enable":false,"number":false,"expand_all":false,"init_open":false},"style":{"primary_color":"#0066cc","logo":"/images/logo.svg","favicon":"/images/logo.svg","avatar":"/images/avatar.svg","font_size":null,"font_family":null,"hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"header_transparent":true,"background_img":"/images/bg.svg","description":"Keep writing and Keep loving.","font_color":null,"hitokoto":true},"scroll":{"progress_bar":false,"percent":false}},"local_search":{"enable":false,"preload":false},"code_copy":{},"code_block":{"tools":{"enable":true,"style":"default"},"highlight_theme":"default"},"side_tools":{},"pjax":{"enable":false},"lazyload":{"enable":false},"comment":{"enable":false,"use":"valine","valine":{"appid":null,"appkey":null,"server_urls":null,"placeholder":null},"gitalk":{"github_id":null,"github_admins":null,"repository":null,"client_id":null,"client_secret":null,"proxy":null},"twikoo":{"env_id":null,"region":null,"version":"1.6.8"},"waline":{"server_url":null,"reaction":false,"version":2}},"post":{"author_label":{"enable":true,"auto":true,"custom_label_list":["Trainee","Engineer","Architect"]},"word_count":{"enable":true,"wordcount":true,"min2read":true},"img_align":"left","copyright_info":false},"version":"3.6.1"}
    KEEP.language_ago = {"second":"%s seconds ago","minute":"%s minutes ago","hour":"%s hours ago","day":"%s days ago","week":"%s weeks ago","month":"%s months ago","year":"%s years ago"}
    KEEP.language_code_block = {"copy":"Copy code","copied":"Copied","fold":"Fold code block","folded":"Folded"}
    KEEP.language_copy_copyright = {"copy":"Copy copyright info","copied":"Copied","title":"Original article title","author":"Original article author","link":"Original article link"}
  </script>
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            
<header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
                <a class="logo-image" href="/">
                    <img src="/images/logo.svg">
                </a>
            
            <a class="logo-title" href="/">
               aaton&#39;s study notes
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                HOME
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                ARCHIVES
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/tags"
                            >
                                TAGS
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/categories"
                            >
                                CATEGORIES
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                ABOUT
                            </a>
                        </li>
                    
                    
                </ul>
            </div>
            <div class="mobile">
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">HOME</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">ARCHIVES</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/tags">TAGS</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/categories">CATEGORIES</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">ABOUT</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="post-page-container">
        <div class="article-content-container">

            <div class="article-title">
                <span class="title-hover-animation">Javascript之日期时间对象</span>
            </div>

            
                <div class="article-header">
                    <div class="avatar">
                        <img src="/images/avatar.svg">
                    </div>
                    <div class="info">
                        <div class="author">
                            <span class="name">aaton</span>
                            
                                <span class="author-label">Lv3</span>
                            
                        </div>
                        <div class="meta-info">
                            
<div class="article-meta-info">
    <span class="article-date article-meta-item">
        
            <i class="fa-regular fa-calendar-plus"></i>&nbsp;
        
        <span class="pc">2024-03-26 11:27:20</span>
        <span class="mobile">2024-03-26 11:27</span>
    </span>
    
        <span class="article-update-date article-meta-item">
        <i class="fas fa-file-pen"></i>&nbsp;
        <span class="pc">2024-03-26 14:41:30</span>
    </span>
    
    
        <span class="article-categories article-meta-item">
            <i class="fas fa-folder"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/Javascript/">Javascript</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/%E6%97%A5%E6%9C%9F%E5%AF%B9%E8%B1%A1/">日期对象</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/tags/Date/">Date</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/tags/%E8%AE%A1%E7%AE%97%E4%B8%A4%E4%B8%AA%E6%97%A5%E6%9C%9F%E7%9B%B8%E5%B7%AE%E5%A4%9A%E5%B0%91%E5%A4%A9/">计算两个日期相差多少天</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
        <span class="article-wordcount article-meta-item">
            <i class="fas fa-file-word"></i>&nbsp;<span>2.5k Words</span>
        </span>
    
    
        <span class="article-min2read article-meta-item">
            <i class="fas fa-clock"></i>&nbsp;<span>10 Mins</span>
        </span>
    
    
        <span class="article-pv article-meta-item">
            <i class="fas fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                        </div>
                    </div>
                </div>
            

            <div class="article-content keep-markdown-body">
                

                <p><img src="https://cn.bing.com/th?id=OHR.AmazonClouds_ZH-CN0578911147_1920x1080.webp&qlt=50" alt="bing.com-巴西亚马逊上空巨大的砧状云" title="bing.com-巴西亚马逊上空巨大的砧状云"></p>
<p>Javascript之日期时间对象 Date 的使用以及相关方法。</p>
<span id="more"></span>

<hr>
<h1 id="1、简介："><a href="#1、简介：" class="headerlink" title="1、简介："></a>1、简介：</h1><p>Date 对象提供了许多方法和属性，可以用于获取和设置日期、时间信息。JavaScript中的 <strong>new Date() 方法用于创建一个新的 Date 对象，该对象表示当前日期和时间</strong>。</p>
<blockquote>
<p>new Date([year, month, day, hour, minute, second, millisecond])<br>其中，每个参数都是可选的。<strong>如果没有指定参数，则 new Date() 方法将创建一个表示当前日期和时间的 Date 对象</strong>。<br>Date做为JavaScript的一种内置对象，必须使用new的方式创建。</p>
</blockquote>
<hr>
<h1 id="2、new-Date-函数"><a href="#2、new-Date-函数" class="headerlink" title="2、new Date() 函数"></a>2、new Date() 函数</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">创建一个表示当前时间的 Date 对象</span><br><span class="line">var nowTime = new Date();</span><br><span class="line"></span><br><span class="line">console.log(nowTime);</span><br><span class="line">// Tue Mar 26 2024 13:17:35 GMT+0800 (中国标准时间)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// 获取 Date 当前时间 年月日时分秒</span><br><span class="line">console.log(nowTime.toLocaleString());</span><br><span class="line">// 2024/3/26 10:41:58</span><br><span class="line"></span><br><span class="line">// 获取 Date 当前时间 年月日</span><br><span class="line">console.log(nowTime.toLocaleDateString());</span><br><span class="line">// 2024/3/26</span><br><span class="line"></span><br><span class="line">// 获取 Date 当前时间 时分秒</span><br><span class="line">console.log(nowTime.toLocaleTimeString());</span><br><span class="line">// 10:41:58</span><br><span class="line"></span><br><span class="line">// 获取 Date 对象的年份</span><br><span class="line">console.log(nowTime.getFullYear());</span><br><span class="line">// 2024</span><br><span class="line"></span><br><span class="line">// 获取 Date 对象的月份（注意月份从0开始，所以使用时需要加1）</span><br><span class="line">console.log(nowTime.getMonth());</span><br><span class="line">// 11</span><br><span class="line"></span><br><span class="line">// 获取 Date 对象的日期</span><br><span class="line">console.log(nowTime.getDate());</span><br><span class="line">// 17</span><br><span class="line"></span><br><span class="line">// 获取 Date 对象的小时</span><br><span class="line">console.log(nowTime.getHours());</span><br><span class="line">// 10</span><br><span class="line"></span><br><span class="line">// 获取 Date 对象的分钟</span><br><span class="line">console.log(nowTime.getMinutes());</span><br><span class="line">// 41</span><br><span class="line"></span><br><span class="line">// 获取 Date 对象的秒</span><br><span class="line">console.log(nowTime.getSeconds());</span><br><span class="line">// 58</span><br><span class="line"></span><br><span class="line">// 获取 Date 对象的毫秒</span><br><span class="line">console.log(nowTime.getMilliseconds());</span><br><span class="line">// 917</span><br></pre></td></tr></table></figure>

<hr>
<h1 id="3、获取-Date-对象时间戳的五种方式"><a href="#3、获取-Date-对象时间戳的五种方式" class="headerlink" title="3、获取 Date 对象时间戳的五种方式"></a>3、获取 Date 对象时间戳的五种方式</h1><p>Date对象在JavaScript内部的表示方式是，距1970年1月1日午夜(GMT时间)的毫秒数(时间戳)，我们在这里也把Date的内部表示形式称为时间戳。</p>
<p>可以使用getTime()将Date对象转换为Date的时间戳，方法setTime()可以把Date的时间戳转换为Date的标准形式。</p>
<h3 id="Date-now"><a href="#Date-now" class="headerlink" title="Date.now()"></a>Date.now()</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">// 获取当前时间戳</span><br><span class="line">console.log(Date.now());        // 1711424981656</span><br></pre></td></tr></table></figure>

<h3 id="Date-parse"><a href="#Date-parse" class="headerlink" title="Date.parse()"></a>Date.parse()</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">// 字符串或者时间对象直接转化成时间戳</span><br><span class="line">Date.parse(new Date());         // 1711425594000</span><br><span class="line">Date.parse(&quot;2024/3/26 12:05&quot;)   // 1711425900000</span><br></pre></td></tr></table></figure>

<blockquote>
<p>不推荐这种办法，毫秒级别的数值被转化为000</p>
</blockquote>
<h3 id="getTime"><a href="#getTime" class="headerlink" title="getTime()"></a>getTime()</h3><p>通过原型方法直接获得当前时间的毫秒值</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">new Date().getTime()            // 1711430230584</span><br></pre></td></tr></table></figure>

<h3 id="valueOf-NaN"><a href="#valueOf-NaN" class="headerlink" title="valueOf()"></a>valueOf()</h3><p>通过valueOf()函数返回指定对象的原始值获得准确的时间戳值</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(new Date()).valueOf()          // 1711425778335</span><br></pre></td></tr></table></figure>

<h3 id="Number"><a href="#Number" class="headerlink" title="Number"></a>Number</h3><p>将时间对象转化为一个number类型的数值，即时间戳</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Number(new Date())              // 1711430441817</span><br></pre></td></tr></table></figure>

<hr>
<h1 id="4、时间戳转时间"><a href="#4、时间戳转时间" class="headerlink" title="4、时间戳转时间"></a>4、时间戳转时间</h1><h3 id="标准时间格式"><a href="#标准时间格式" class="headerlink" title="标准时间格式"></a>标准时间格式</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">new Date(1711430441817)         // Tue Mar 26 2024 13:20:41 GMT+0800 (中国标准时间)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>时间戳参数必须是Number类型，如果是字符串，解析结果：Invalid Date</p>
</blockquote>
<h3 id="‘2024-x2F-3-x2F-26-13-20’格式"><a href="#‘2024-x2F-3-x2F-26-13-20’格式" class="headerlink" title="‘2024&#x2F;3&#x2F;26 13:20’格式"></a>‘2024&#x2F;3&#x2F;26 13:20’格式</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function getLocalTime(n) &#123;</span><br><span class="line">    return new Date(parseInt(n)).toLocaleString();</span><br><span class="line">&#125;</span><br><span class="line">getLocalTime(1711430441817)     // &#x27;2024/3/26 13:20:41&#x27;</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(new Date()).toLocaleDateString() + &quot; &quot; + (new Date()).toLocaleTimeString()</span><br><span class="line">// &#x27;2024/3/26 13:20:41&#x27;</span><br></pre></td></tr></table></figure>

<h3 id="‘yyyy-MM-dd-hh-mm-ss’格式"><a href="#‘yyyy-MM-dd-hh-mm-ss’格式" class="headerlink" title="‘yyyy-MM-dd hh:mm:ss’格式"></a>‘yyyy-MM-dd hh:mm:ss’格式</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function getData(n)&#123;</span><br><span class="line">  n=new Date(n)</span><br><span class="line">  return n.toLocaleDateString().replace(/\//g, &quot;-&quot;) + &quot; &quot; + n.toTimeString().substr(0, 8)</span><br><span class="line">&#125;</span><br><span class="line">getData(1711430441817)          // &#x27;2024-3-26 13:20:41&#x27;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在某些浏览器上会出现不理想的效果，因为toLocaleDateString()方法是因浏览器而异的，比如 IE为”2016年8月24日 22:26:19”格式 ;搜狗为”Wednesday, August 24, 2016 22:39:42”</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">function getData(n) &#123;</span><br><span class="line">  let now = new Date(n),</span><br><span class="line">    y = now.getFullYear(),</span><br><span class="line">    m = now.getMonth() + 1,</span><br><span class="line">    d = now.getDate();</span><br><span class="line">  return y + &quot;-&quot; + (m &lt; 10 ? &quot;0&quot; + m : m) + &quot;-&quot; + (d &lt; 10 ? &quot;0&quot; + d : d) + &quot; &quot; + now.toTimeString().substr(0, 8);</span><br><span class="line">&#125;</span><br><span class="line">getData(1642471746435)          // &#x27;2024-3-26 13:20:41&#x27;</span><br></pre></td></tr></table></figure>

<hr>
<h1 id="5、Date-对象构造函数"><a href="#5、Date-对象构造函数" class="headerlink" title="5、Date 对象构造函数"></a>5、Date 对象构造函数</h1><h3 id="new-Date-year-month-day-hours-minutes-seconds-microseconds"><a href="#new-Date-year-month-day-hours-minutes-seconds-microseconds" class="headerlink" title="new Date(year, month, day, hours, minutes, seconds, microseconds)"></a>new Date(year, month, day, hours, minutes, seconds, microseconds)</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">new Date(2024, 3, 26, 11, 12, 13, 14);</span><br><span class="line">// Fri Apr 26 2024 11:12:13 GMT+0800 (中国标准时间)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>参数说明：<br>year - 四位数的年份，如果取值为0-99，则在其之上加上1900<br>month - 0(代表一月)-11(代表十二月)之间的月份<br>day - 1-31之间的日期<br>hours - 0(代表午夜)-23之间的小时数<br>minutes - 0-59之间的分钟数<br>seconds - 0-59之间的秒数<br>microseconds - 0-999之间的毫秒数</p>
</blockquote>
<h3 id="new-Date-milliseconds"><a href="#new-Date-milliseconds" class="headerlink" title="new Date(milliseconds)"></a>new Date(milliseconds)</h3><p>milliseconds：距离JavaScript内部定义的起始时间1970年1月1日的毫秒数</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">new Date(1711433136733)</span><br><span class="line">// Tue Mar 26 2024 14:05:36 GMT+0800 (中国标准时间)</span><br></pre></td></tr></table></figure>
<p>如果参数为一个数字，将数字视为毫秒值，转换为日期</p>
<h3 id="new-Date-datestring"><a href="#new-Date-datestring" class="headerlink" title="new Date(datestring)"></a>new Date(datestring)</h3><p>datestring：字符串代表的日期与时间</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">new Date(&quot;2009-08-08 12:12:12&quot;)</span><br><span class="line">// Sat Aug 08 2009 12:12:12 GMT+0800 (中国标准时间)</span><br></pre></td></tr></table></figure>
<p>如果参数为一个字符串，将字符串视为日期的字符串表示，转换为日期</p>
<h1 id="6、Date-函数"><a href="#6、Date-函数" class="headerlink" title="6、Date 函数"></a>6、Date 函数</h1><h3 id="日期获取类函数"><a href="#日期获取类函数" class="headerlink" title="日期获取类函数"></a>日期获取类函数</h3><p><strong>Date()</strong>                      – Date对象的构造函数<br><strong>getDate()</strong>                   – 返回date对象中的月份中的天数(1-31)<br><strong>getDay()</strong>                    – 返回date对象中的星期中的天数(0-6)<br><strong>getFullYear()</strong>               – 返回date对象中的四位数年份<br><strong>getHours()</strong>                  – 返回date对象中的小时数(0-23)<br><strong>getMilliseconds()</strong>           – 返回date对象中的毫秒数(0-999)<br><strong>getMinutes()</strong>                – 返回date对象中的分钟数(0-59)<br><strong>getMonth()</strong>                  – 返回date对象中的月份数(0-11)<br><strong>getSeconds()</strong>                – 返回date对象中的秒数(0-59)<br><strong>getTime()</strong>                   – 返回date对象的时间戳表示法(毫秒表示)<br><strong>getTimezoneOffset()</strong>         – 返回本地时间与用UTC表示当前日期的时间差，以分钟为单位<br><strong>getUTCDate()</strong>                – 返回date对象中用世界标准时间(UTC)表示的月份中的一天(1-31)<br><strong>getUTCDay()</strong>                 – 返回date对象中用世界标准时间(UTC)表示的周中的一天(0-6)<br><strong>getUTCFullYear()</strong>            – 返回date对象中用世界标准时间(UTC)表示的四位年份<br><strong>getUTCHours()</strong>               – 返回date对象中用世界标准时间(UTC)表示的小时数(0-23)<br><strong>getUTCMilliseconds()</strong>        – 返回date对象中用世界标准时间(UTC)表示的毫秒数(0-999)<br><strong>getUTCMinutes()</strong>             – 返回date对象中用世界标准时间(UTC)表示的分钟数(0-59)<br><strong>getUTCMonth()</strong>               – 返回date对象中用世界标准时间(UTC)表示的月份数(0-11)<br><strong>getUTCSeconds()</strong>             – 返回date对象中用世界标准时间(UTC)表示的秒数(0-59)<br><strong>getYear()</strong>                   – 返回date对象的年份(真实年份减去1900)<br><strong>Date.UTC()</strong>                  – 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)</p>
<h3 id="日期设置类函数"><a href="#日期设置类函数" class="headerlink" title="日期设置类函数"></a>日期设置类函数</h3><p><strong>setDate()</strong>                   – 设置date对象中月份的一天，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setFullYear()</strong>               – 设置date对象中的年份，月份和天，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setHours()</strong>                  – 设置date对象的小时，分钟，秒和毫秒，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setMilliseconds()</strong>           – 设置date对象的毫秒数，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setMinutes()</strong>                – 设置date对象的分钟，秒，毫秒，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setMonth()</strong>                  – 设置date对象中月份，天，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setSeconds()</strong>                – 设置date对象中月份的一天，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setTime()</strong>                   – 使用毫秒数设置date对象，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setUTCDate()</strong>                – 设置date对象中用世界标准时间(UTC)表示的月份的一天，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setUTCFullYear()</strong>            – 设置date对象中用世界标准时间(UTC)表示的年份，月份和天，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setUTCHours()</strong>               – 设置date对象中用世界标准时间(UTC)表示的小时，分钟，秒和毫秒，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setUTCMilliseconds()</strong>        – 设置date对象中用世界标准时间(UTC)表示的毫秒数，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setUTCMinutes()</strong>             – 设置date对象中用世界标准时间(UTC)表示的分钟，秒，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setUTCMonth()</strong>               – 设置date对象中用世界标准时间(UTC)表示的月份，天，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setUTCSeconds()</strong>             – 设置date对象中用世界标准时间(UTC)表示的秒，毫秒，并返回date对象距1970年1月1日午夜之间的毫秒数(时间戳)<br><strong>setYear()</strong>                   – 设置date对象的年份(真实年份减去1900)</p>
<h3 id="日期打印类函数"><a href="#日期打印类函数" class="headerlink" title="日期打印类函数"></a>日期打印类函数</h3><p><strong>toDateString()</strong>              – 返回date对象的日期部分的字符串表示<br><strong>toGMTString()</strong>               – 返回date对象的格林威治时间(GMT)的字符串表示<br><strong>toLocaleDateString</strong>          – 返回date对象的日期部分的本地化字符串<br><strong>toLocaleTimeString</strong>          – 返回date对象的时间部分的本地化字符串<br><strong>toTimeString()</strong>              – 返回date对象的时间部分的字符串<br><strong>toUTCString</strong>                 – 返回date对象的世界标准时间(UTC)的字符串表示</p>
<h1 id="7、实例"><a href="#7、实例" class="headerlink" title="7、实例"></a>7、实例</h1><h3 id="计算两个日期相差多少天"><a href="#计算两个日期相差多少天" class="headerlink" title="计算两个日期相差多少天"></a>计算两个日期相差多少天</h3><p>有时候，我们需要知道两个日期之间相差多少天。<br>可以先将日期转换为毫秒数，然后相减并除以一天的毫秒数（1000 * 60 * 60 * 24）来得到结果。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">function daysBetween(date1, date2) &#123;</span><br><span class="line">  let oneDay = 1000 * 60 * 60 * 24;</span><br><span class="line">  return Math.round((date2 - date1) / oneDay);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">let start = new Date(&#x27;June 20, 2022&#x27;);</span><br><span class="line">let end = new Date(&#x27;July 20, 2022&#x27;);</span><br><span class="line"></span><br><span class="line">console.log(daysBetween(start, end));       // &quot;30&quot;</span><br></pre></td></tr></table></figure>

<h3 id="计算两个日期之间的时间差"><a href="#计算两个日期之间的时间差" class="headerlink" title="计算两个日期之间的时间差"></a>计算两个日期之间的时间差</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">const startDate = new Date(&#x27;2021-01-01&#x27;);</span><br><span class="line">const endDate = new Date(&#x27;2021-12-31&#x27;);</span><br><span class="line"></span><br><span class="line">const timeDiff = endDate - startDate;</span><br><span class="line"></span><br><span class="line">// 计算两个日期之间的天数</span><br><span class="line">const daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));</span><br><span class="line"></span><br><span class="line">// 计算两个日期之间的小时数</span><br><span class="line">const hoursDiff = Math.floor(timeDiff / (1000 * 60 * 60));</span><br><span class="line"></span><br><span class="line">// 计算两个日期之间的分钟数</span><br><span class="line">const minutesDiff = Math.floor(timeDiff / (1000 * 60));</span><br><span class="line"></span><br><span class="line">// 计算两个日期之间的秒数</span><br><span class="line">const secondsDiff = Math.floor(timeDiff / 1000);</span><br></pre></td></tr></table></figure>

<hr>
<p><em><strong>参考文献</strong></em></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_65793170/article/details/134459673" >CSDN : JavaScript中的时间日期函数new Date()（JS中5种获取时间戳的函数）<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://juejin.cn/post/7182121943720525885" >稀土掘金 ： 5种获取JavaScript时间戳函数的方法<i class="fas fa-external-link-alt"></i></a></p>

            </div>

            

            
                <ul class="post-tags-box">
                    
                        <li class="tag-item">
                            <a href="/tags/%E6%97%A5%E6%9C%9F%E5%AF%B9%E8%B1%A1/">#日期对象</a>&nbsp;
                        </li>
                    
                        <li class="tag-item">
                            <a href="/tags/Date/">#Date</a>&nbsp;
                        </li>
                    
                        <li class="tag-item">
                            <a href="/tags/%E8%AE%A1%E7%AE%97%E4%B8%A4%E4%B8%AA%E6%97%A5%E6%9C%9F%E7%9B%B8%E5%B7%AE%E5%A4%9A%E5%B0%91%E5%A4%A9/">#计算两个日期相差多少天</a>&nbsp;
                        </li>
                    
                </ul>
            

            
                <div class="article-nav">
                    
                        <div class="article-prev">
                            <a class="prev"
                               rel="prev"
                               href="/2024/03/29/Vue2%E4%B9%8B%E6%95%B0%E6%8D%AE%E5%8F%8C%E5%90%91%E7%BB%91%E5%AE%9A%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/"
                            >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                                <span class="title flex-center">
                                <span class="post-nav-title-item"></span>
                                <span class="post-nav-item">Prev posts</span>
                            </span>
                            </a>
                        </div>
                    
                    
                        <div class="article-next">
                            <a class="next"
                               rel="next"
                               href="/2024/03/26/JavaScript%E5%B8%B8%E7%94%A8%E5%85%AC%E5%85%B1%E6%96%B9%E6%B3%95%E4%B9%8B%E6%97%B6%E9%97%B4%E8%BD%AC%E6%95%B0%E5%AD%97/"
                            >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">JavaScript常用公共方法之时间转数字</span>
                                <span class="post-nav-item">Next posts</span>
                            </span>
                                <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                            </a>
                        </div>
                    
                </div>
            

            
        </div>

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            
<footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
                <span>2020</span> -
            
            2024
            
                &nbsp;<i class="fas fa-heart icon-animate"></i>
                &nbsp;<a href="/">aaton</a>
            
        </div>
        
            <script async 
                    src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    Visitor Count&nbsp;<span id="busuanzi_value_site_uv"></span>&ensp;
                
                
                    Totalview&nbsp;<span id="busuanzi_value_site_pv"></span>
                
            </div>
        
        <div class="theme-info info-item">
            Powered by <a target="_blank" href="https://hexo.io">Hexo</a>&nbsp;|&nbsp;Theme&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.6.1</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    <div class="zoom-in-image-mask">
    <img class="zoom-in-image">
</div>


    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>







    
<script src="/js/code-block.js"></script>





<div class="post-scripts">
    
        
<script src="/js/post-helper.js"></script>

        
        
    
</div>



</body>
</html>
